﻿using GameControl;
using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace GameMap
{

    public class MapManager : LogicModuleBase, IMapManager
    {

        public static float meshSize = 1;
        public static float itemSize = 0.5f;
        public static Bounds MapBounds = new Bounds(Vector3.zero, new Vector3(256, 256,256));
        Vector3Int mapSize = new Vector3Int(64, 64, 1);
        List<ItemField> sceneItems = new List<ItemField>();

        public float MeshSize => meshSize;

        public Vector3Int MapSize => mapSize;

        public List<ItemField> SceneItems => sceneItems;




        public override void Init(object args)
        {
            base.Init(args);
            var tp = args as ItemsData;
            if(args!=null)
            {
                sceneItems = tp.ItemsInMap;
            }
       //     InitItemOnScene();
            MessageManager.Instance.Get<ItemMapEvent>().RegisterHandler(ItemMapEvent.DeleteInMap, DeleteInMap);
            MessageManager.Instance.Get<ItemMapEvent>().RegisterHandler(ItemMapEvent.CreateInMap, CreateInMap);
        }
        EventArgs CreateInMap(object sender, EventArgs e)
        {
            var info = e as ItemCreateArgs;
            var field = GameManager.Get<IItemInstantiateManager>().CreatItemField(info.ItemName);
            if (field == null)
            {
                Debug.LogWarning($"{ info.ItemName}不存在");
                return null;
            }
            sceneItems.Add(field);
            field.Position = info.Position;
            /* MessageManager.Instance.Get<ItemSyncMsg>().DispatchMessage(ItemSyncMsg.ForceSyncInstance, this, null);
             if (ClockManager.Get<IItemSyncManager>().GetGameObjectByName(field.ItemName) == null)
             {
                 Debug.LogError("!!!");
             }*/
            info.ItemInit(field);
      //      ClockManager.Get<IItemSyncManager>().AddItemInitAction(field.ItemName, info.ItemInit);
            return new UniversalEventArgs(field.ItemName);
        }
        System.EventArgs DeleteInMap(object sender,System.EventArgs e)
        {
            sceneItems.RemoveAll(s=>s.ItemName==(e as ItemMapArgs).ItemName);
            GameManager.Get<IItemInstantiateManager>().DestoryItemField((e as ItemMapArgs).ItemName);
            return null;
        }
        //List<string> UsableBench = new List<string>();
      
        public void DestoryAllItemOnScene()
        {
            foreach(var x in sceneItems)
            {
                GameManager.Get<IItemInstantiateManager>().DestoryItemField(x.ItemName);
            }
            sceneItems.Clear();
        }

        public List<ItemField> FindAllPlayerItem()
        {
            var ans =new  List<ItemField>();
            foreach(var x in sceneItems)
            {
                if (x is CombinableItemField e)
                {
                    if (e.IsPlayer)
                    {
                        ans.Add(e);
                    }
                }
            }
            return ans;
        }

        public PosFixedItemField GetPosFixedItemFieldByMeshPos(Vector3Int pos)
        {
            try
            {
                var list = sceneItems.FindAll(s => (s is PosFixedItemField) && (s as PosFixedItemField).MeshPosition == pos);
                if (list.Count > 1)
                {
                    throw new Exception($"{pos}有物体世界坐标重叠");
                }else if (list.Count == 0)
                {
                    return null;
                }
                return sceneItems.FindAll(s => (s is PosFixedItemField) && (s as PosFixedItemField).MeshPosition == pos)[0]as PosFixedItemField;

            }catch(Exception e)
            {
                Debug.LogError(e);
                return null;
            }
        }
    }
    public interface IMapManager
    {
        float MeshSize { get; }
        Vector3Int MapSize { get; }
         List<ItemField> SceneItems { get; }

        PosFixedItemField GetPosFixedItemFieldByMeshPos(Vector3Int pos);
        void DestoryAllItemOnScene();
        List<ItemField> FindAllPlayerItem();
       // string GetNewBenchName { get; }
    }

}